package com.cssl.controller;

import java.sql.SQLException;

import javax.annotation.PostConstruct;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cssl.pojo.Users;
import com.cssl.service.UsersService;
import com.cssl.vo.UsersVo;
import com.github.pagehelper.Page;

@Controller
public class UsersController {
	
	@PostConstruct
	public void init() {
		System.out.println("初始化。。。。。。。。。。。。。。");
	}
	
	@Autowired
	private UsersService service;
	
	@GetMapping("/hello")
	public String hello() {
		System.out.println("hello");
		int i = 1/0;
		return "redirect:success.html";
	}
	
	@ResponseBody
	@GetMapping("/query/{pageIndex}/{pageSize}")
	public Page<Users> query(@PathVariable int pageIndex,@PathVariable int pageSize){
		return service.findPage(pageIndex, pageSize);
	}

	@PostMapping("/form")
	public String form(UsersVo uvo) throws SQLException {
		System.out.println("form:"+service);
		System.out.println("form:"+uvo);
		
		Users user = new Users();
		BeanUtils.copyProperties(uvo, user);
		if(service.saveUsers(user))
			return "success";
		return "form";
	}
	
	
	
}
